package com.craney.SampleLoadXML
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class SampleLoadXML
	{
		private var titleText:TextField = new TextField();
		private var parentSprite:Sprite;
		private var flag:Boolean = false;
		private var start:Number;
		private var t:Number;
		private var lastLoaded:uint = 0;
		private var totalTime:Number = 0;
		
		public function SampleLoadXML(ps:Sprite)
		{
			
			var targetURL:String = "D:\\备份\\aptana_update_009212.zip";
			var targetVarialbes:String = "you.txt";
			var request:URLRequest = new URLRequest(targetVarialbes);
			var loader:URLLoader = new URLLoader();
			ps.addChild(titleText);
			parentSprite = ps;
			loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			loader.addEventListener(Event.COMPLETE, completeHandler);
			loader.addEventListener(ProgressEvent.PROGRESS, prograssHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			try
			{
				trace("Loading...");
				start = t = new Date().time;
				loader.load(request);
				trace("after...");
			}
			catch(error:Error)
			{
				trace("Unalble to load URL:" + error);
			}
			finally
			{
				trace("finally");
			}
			titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.border = true;
		}
		
		private function prograssHandler(evt:Event):void
		{
//			var now:Number = new Date().time;
////			trace("Time now:\t" + now);
//			trace("Time Block:\t" + (now-t));
//			totalTime += now-t;
//			t = now;
//			var prograss:ProgressEvent = evt as ProgressEvent;
////			trace("Have loaded:" + prograss.bytesLoaded);
//			trace("Block byte:" + ((prograss.bytesLoaded-lastLoaded)/1024) + "KB");
//			lastLoaded = prograss.bytesLoaded;
//			trace(prograss.bytesLoaded/prograss.bytesTotal*100 + "%");
		}
		
		private function ioErrorHandler(evt:Event):void
		{
			trace(evt.target, evt.type, evt);
		}
		
		private function convertHandler(evt:Event):void
		{
			trace("converted!");
			parentSprite.addChild(evt.target.loader as Loader);
			evt.target.loader.x = flag ? 140 : flag=!flag, 0;
		}
		private function completeHandler(evt:Event):void
		{
			trace("loaded!");
//			trace("all time:\t" + (new Date().time-start));
//			trace("all time2:\t" + totalTime);
			var load:URLLoader = evt.target as URLLoader;
			var data:URLVariables = load.data;
			var s:String = data.message;
			var a:Array = s.split("*");
			trace(a.length);
			trace(a);
			
			
//			var loaders:Array = [new Loader(), new Loader()];
//			var content:ByteArray = evt.target.data as ByteArray;
//			loaders[0].contentLoaderInfo.addEventListener(Event.COMPLETE, convertHandler);
//			loaders[1].contentLoaderInfo.addEventListener(Event.COMPLETE, convertHandler);
//			loaders[0].loadBytes(content);
//			loaders[1].loadBytes(content);
			
//			var result:String = evt.target.data;
//			trace(result);
//			titleText.text = result; 
//			for(var i:String in evt.target.data)
//			{
//				trace(i + "\t:" + evt.target.data[i]);
//			}
//			var resultXML:XML = new XML(evt.target.data);
//			var titleList:XMLList = resultXML.channel.item.title;
//			for each(var tilte:XML in titleList)
//			{
//				titleText.appendText("*" + tilte + "\n");
//			}
//			trace(evt.target.data);
		}

	}
}